//
//  ALDrop.m
//  Ummy.G
//
//  Created by 安林 on 16/10/2.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALDrop.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import "ALOneLeftModel.h"
#import "ALTableViewCell.h"
#import "ALOneRightModel.h"


@implementation ALDrop

//“商家”界面下拉菜单的实现
+ (instancetype)drop{
    
    return [[[NSBundle mainBundle]loadNibNamed:@"ALDrop" owner:nil options:nil]lastObject];
}

-(NSMutableArray *)OneLeftArray{
    if (!_OneLeftArray) {
        _OneLeftArray = [NSMutableArray array];
    }
    return _OneLeftArray;
}

-(NSMutableArray *)OneRightArray{
    if (!_OneRightArray) {
        _OneRightArray = [NSMutableArray array];
    }
    return _OneRightArray;
}



-(void)awakeFromNib{
    [self addloadData];
//    ALTableView *ta = [[ALTableView alloc]init];
//    self.delegate = ta;
    _tableView1.delegate = self;
    _tableView1.dataSource =self;
    _tableView2.delegate = self;
    _tableView2.dataSource = self;
}


#pragma mark 加载数据
-(void)addloadData{

    //请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://api.meituan.com/group/v1/poi/cates/showlist?__skck=40aaaf01c2fc4801b9c059efcd7aa146&__skcy=hSjSxtGbfd1QtKRMWnoFV4GB8jU%3D&__skno=0DEF926E-FB94-43B8-819E-DD510241BCC3&__skts=1436504818.875030&__skua=bd6b6e8eadfad15571a15c3b9ef9199a&__vhost=api.mobile.meituan.com&ci=1&cityId=1&client=iphone&movieBundleVersion=100&msid=48E2B810-805D-4821-9CDD-D5C9E01BC98A2015-07-10-12-44726&userid=10086&utm_campaign=AgroupBgroupD100Fa20141120nanning__m1__leftflow___ab_pindaochangsha__a__leftflow___ab_gxtest__gd__leftflow___ab_gxhceshi__nostrategy__leftflow___ab_i550poi_ktv__d__j___ab_chunceshishuju__a__a___ab_gxh_82__nostrategy__leftflow___ab_i_group_5_3_poidetaildeallist__a__b___b1junglehomepagecatesort__b__leftflow___ab_gxhceshi0202__b__a___ab_pindaoquxincelue0630__b__b1___ab_i550poi_xxyl__b__leftflow___ab_i_group_5_6_searchkuang__a__leftflow___i_group_5_2_deallist_poitype__d__d___ab_pindaoshenyang__a__leftflow___ab_b_food_57_purepoilist_extinfo__a__a___ab_waimaiwending__a__a___ab_waimaizhanshi__b__b1___ab_i550poi_lr__d__leftflow___ab_i_group_5_5_onsite__b__b___ab_xinkeceshi__b__leftflowGmerchant&utm_content=4B8C0B46F5B0527D55EA292904FD7E12E48FB7BEA8DF50BFE7828AF7F20BB08D&utm_medium=iphone&utm_source=AppStore&utm_term=5.7&uuid=4B8C0B46F5B0527D55EA292904FD7E12E48FB7BEA8DF50BFE7828AF7F20BB08D&version_name=5.7";
    //请求数据
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"data"];
        NSArray *arrayR = responseObject[@"list"];
        for (NSDictionary *dict in array) {
            ALOneLeftModel *model = [ALOneLeftModel oneLeftWithDict:dict];
           
            [self.OneLeftArray addObject:model];
        }
        
        for (NSDictionary *dict in arrayR) {
             ALOneRightModel *modelR = [ALOneRightModel oneRightWithDict:dict];
            [self.OneRightArray addObject:modelR];

        }
        //请求成功把指示器销毁
        [SVProgressHUD dismiss];
        [self.tableView1 reloadData];
        [self.tableView2  reloadData];
        
        //1：默认选中左边表格的第0行
        [self tableView:self.tableView1 didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        
        //写成桌面plist文件
        //[responseObject writeToFile:@"/Users/anlin/Desktop/model.plist" atomically:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败的指示器
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        //NSLog(@"%@",error);
    }];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return self.OneLeftArray.count;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    // NSLog(@"123");
     ALOneLeftModel *model = self.OneLeftArray[indexPath.row];
     
     static NSString *ID = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
     // 左边表格
    if (cell == nil) {
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    
        cell.textLabel.text = model.name;
        //cell.textLabel.text = model.subname;
        
        }
     return cell;
 }





//监听选中哪行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//     NSLog(@"12345");
//    ALOneLeftModel *model = [[ALOneLeftModel alloc]init];
//    [self.delegate dDropWithString:model.subnames];
    
    
    ALOneLeftModel *model = self.OneLeftArray[indexPath.row];
    

}

@end
